<?php
/*
 > [ACTIVE] Name Category Points CUSTOM_ACHIEVEMENT?
   (edit)

 > list & add special achievements separately.

 > also rename of categories
 */

/********************************************************************************
 * Screen Functions
 *
 * Screen functions are the controllers of BuddyPress. They will execute when their
 * specific URL is caught. They will first save or manipulate data using business
 * functions, then pass on the user to a template file.
 */
function dpa_add_admin_menu() {
	global $wpdb, $bp;

	if ( !is_site_admin() )
		return false;

	add_submenu_page( 'bp-general-settings', __( 'Achievements', 'dpa'), __( 'Achievements', 'dpa' ), 'manage_options', $bp->achievements->id, 'dpa_screen_admin_settings' );
}
add_action( 'admin_menu', 'dpa_add_admin_menu' );

function dpa_admin_settings_edit_form( $achievement_id ) {
	global $bp, $wpdb;
	
	$achievement = $wpdb->get_row( $wpdb->prepare( "SELECT category_id, description, nice_name, points FROM {$bp->achievements->table_achievements} WHERE id=%d LIMIT 1", $achievement_id ) );
	if ( !$achievement ) {
		bp_core_add_message( __( 'There was an error accessings the database; please try again.', 'dpa' ), 'error' );
		return;
	}
?>
	<tr id="edit">
		<td></td>
		<td colspan="3">
			<form action="<?php echo site_url() . "/wp-admin/admin.php?page=achievements&amp;action=save&amp;achievement=$achievement_id" ?>" method="post">
				<?php wp_nonce_field( 'achievement_save' ) ?>
				<div class="column">
					<h3><label for="achievement_name"><?php _e( 'Name', 'dpa' ) ?></label></h3>
					<input type="text" name="achievement_name" id="achievement_name" tabindex="1" autocomplete="off" value="<?php echo esc_attr( apply_filters( 'dpa_admin_settings_name_edit_value', $achievement->nice_name ) ) ?>" />
					<h3><label for="achievement_description"><?php _e( 'Description', 'dpa' ) ?></label></h3>
					<textarea name="achievement_description" id="achievement_description" rows="3" tabindex="2"><?php echo esc_attr( apply_filters( 'dpa_admin_settings_description_edit_value', $achievement->description ) ) ?></textarea>
				</div>

				<div class="column">
					<h3><label for="achievement_category"><?php _e( 'Category', 'dpa' ) ?></label></h3>
					<select name="achievement_category" id="achievement_category">
						<?php $categories = $wpdb->get_results( $wpdb->prepare( "SELECT category, id FROM {$bp->achievements->table_categories}" ) ); ?>
						<?php foreach ( $categories as $category ) { ( $category->id == $achievement->category_id) ? $selected = ' selected' : $selected = ''; ?>
						<option value="<?php echo esc_attr( apply_filters( 'dpa_admin_settings_categoryid_edit_value', $category->id ) ) ?>" <?php echo $selected ?>><?php echo esc_attr( apply_filters( 'dpa_admin_settings_category_edit_value', $category->category ) ) ?></option>
						<?php } ?>
					</select>

					<h3><label for="achievement_points">Points</label></h3>
					<input type="text" name="achievement_points" id="achievement_points" tabindex="1" type="number" autocomplete="off" value="<?php echo esc_attr( apply_filters( 'dpa_admin_settings_points_edit_value', $achievement->points ) ) ?>" />

					<div class="controls">
						<input type="reset" value="Reset" class="button" type="reset" />
						<input type="submit" tabindex="5" class="button-primary" value="<?php _e( 'Save Changes', 'dpa' ) ?>" />
					</div>
				</div>

				<div style="clear: both"></div>
			</form>
		</td>
	</tr>
<?php
}

function dpa_screen_admin_settings() {
	global $wpdb, $bp;

	if ( !is_site_admin() )
		return;

	if ( isset( $_REQUEST['achievement'] ) && isset( $_REQUEST['action'] ) ) {
		if ( 'enable' == $_REQUEST['action'] )
			$action = 'enable';
		elseif ( 'disable' == $_REQUEST['action'] )
			$action = 'disable';
		elseif ( 'edit' == $_REQUEST['action'] )
			$action = 'edit';
		elseif ( 'save' == $_REQUEST['action'] )
			$action = 'save';
		else {
			bp_core_redirect( $bp->root_domain );
			die;
		}

		$updated_achievement_id = (int) $_REQUEST['achievement'];
		bp_core_setup_message();  // To trigger error/success feedback; doesn't usually work on admin pages.

		if ( 'enable' == $action || 'disable' == $action || 'save' == $action ) {
			check_admin_referer( "achievement_$action" );

			wp_cache_delete( 'dpa_distinct_categories', 'dpa' );
			dpa_update_achievement( $updated_achievement_id, $action );
			dpa_recalculate_userpoints();
		}
	}
?>
	<div class="wrap">
		<div id="icon-options-general" class="icon32"><br /></div>
		<h2><?php _e( 'Achievements', 'dpa' ) ?></h2>

		<?php do_action( 'template_notices' ); // (error/success feedback) ?>
		<h3><?php _e( 'Currently Active Achievements', 'dpa' ) ?></h3>
		<p><?php _e( 'Achievements that appear in the list below are active for the entire site.', 'dpa' ) ?></p>

		<table class="widefat achievements-enabled" cellspacing="0">
			<thead>
				<tr>
					<th scope="row" class="check-column">&nbsp;</th>
					<th scope="col"><?php _e( 'Achievement', 'dpa' ) ?></th>
					<th scope="col"><?php _e( 'Description', 'dpa' ) ?></th>
					<th scope="col"><?php _e( 'Action', 'dpa' ) ?></th>
				</tr>
			</thead>

			<tbody class="plugins">
				<?php if ( dpa_has_achievements( array( 'type' => 'enabled' ) ) ) : while ( dpa_user_achievements() ) : dpa_the_achievement() ?>

				<tr>
					<th scope="row" class="check-column">&nbsp;</th>
					<td class="name"><?php dpa_achievement_name() ?></td>
					<td class="desc"><p><?php dpa_achievement_description() ?></p></td>
					<td><a href="<?php echo wp_nonce_url( site_url() . '/wp-admin/admin.php?page=achievements&amp;action=disable&amp;achievement=' . dpa_get_achievement_id(), 'achievement_disable' ) ?>"><?php _e( 'Disable', 'dpa' ) ?></a> | <a href="<?php echo site_url() . '/wp-admin/admin.php?page=achievements&amp;action=edit&amp;achievement=' . dpa_get_achievement_id() ?>"><?php _e( 'Edit', 'dpa' ) ?></a>
					<?php if ( isset( $action ) && isset( $updated_achievement_id ) && 'edit' == $action && $updated_achievement_id == dpa_get_achievement_id() ) dpa_admin_settings_edit_form( $updated_achievement_id ) ?>
					</td>
				</tr>

				<?php endwhile; endif; ?>
			</tbody>

			<tfoot>
				<tr>
					<th scope="row" class="check-column">&nbsp;</th>
					<th scope="col"><?php _e( 'Achievement', 'dpa' ) ?></th>
					<th scope="col"><?php _e( 'Description', 'dpa' ) ?></th>
					<th scope="col"><?php _e( 'Action', 'dpa' ) ?></th>
				</tr>
			</tfoot>
		</table>

		<h3><?php _e( 'Other Achievements', 'dpa' ) ?></h3>
	
		<table class="widefat achievements-disabled" cellspacing="0">
			<thead>
				<tr>
					<th scope="row" class="check-column">&nbsp;</th>
					<th scope="col"><?php _e( 'Achievement', 'dpa' ) ?></th>
					<th scope="col"><?php _e( 'Description', 'dpa' ) ?></th>
					<th scope="col"><?php _e( 'Action', 'dpa' ) ?></th>
				</tr>
			</thead>

			<tbody class="plugins">
				<?php if ( dpa_has_achievements( array( 'type' => 'disabled' ) ) ) : while ( dpa_user_achievements() ) : dpa_the_achievement() ?>

				<tr>
					<th scope="row" class="check-column">&nbsp;</th>
					<td class="name"><?php dpa_achievement_name() ?></td>
					<td class="desc"><p><?php dpa_achievement_description() ?></p></td>
					<td><a href="<?php echo wp_nonce_url( site_url() . '/wp-admin/admin.php?page=achievements&amp;action=enable&amp;achievement=' . dpa_get_achievement_id(), 'achievement_enable' ) ?>"><?php _e( 'Enable', 'dpa' ) ?></a> | <a href="<?php echo site_url() . '/wp-admin/admin.php?page=achievements&amp;action=edit&amp;achievement=' . dpa_get_achievement_id() ?>"><?php _e( 'Edit', 'dpa' ) ?></a></td>
				</tr>

				<?php endwhile; endif; ?>
			</tbody>

			<tfoot>
				<tr>
					<th scope="row" class="check-column">&nbsp;</th>
					<th scope="col"><?php _e( 'Achievement', 'dpa' ) ?></th>
					<th scope="col"><?php _e( 'Description', 'dpa' ) ?></th>
					<th scope="col"><?php _e( 'Action', 'dpa' ) ?></th>
				</tr>
			</tfoot>
		</table>

	</div>
<?php
}
?>